﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Web;

namespace MyLib
{
    internal class SqlMapConfig
    {
        public static string GetConfig()
        {
            StringBuilder sb = new StringBuilder();
            string sqlMaps = "";
            object section = ConfigurationManager.GetSection("ibatis");
            if (section != null)
            {
                XmlNode x = (XmlNode)section;
                XmlNodeList list = x.ChildNodes;
                foreach (XmlNode xn in list)
                {
                    XmlAttribute attr = xn.Attributes["uri"];
                    string uri = attr.InnerText;
                    if (uri.StartsWith("file://"))
                    {
                        XmlDocument xmldoc = new XmlDocument();
                        string path = uri.Remove(0, 6);
                        if (HttpContext.Current != null)
                        {
                            path = HttpContext.Current.Server.MapPath("~")+path;
                        }
                        //加载所有数据配置文件
                        if (!string.IsNullOrEmpty(path))
                        {
                            xmldoc.Load(path);
                            XmlNode node = xmldoc.SelectSingleNode("//sqlMaps");
                            if (node != null)
                            {
                                sb.Append(node.InnerXml);
                            }
                        }
                    }

                    if (uri.StartsWith("assembly://"))
                    {
                        XmlDocument xmldoc = new XmlDocument();
                        string str = uri.Remove(0, 11);
                        string[] szz = str.Split('/');
                        Stream stream = ResourceUtil.GetResourceStream(szz[0], str.Replace("/", "."));
                        //加载所有数据配置文件
                        if (stream != null)
                        {
                            xmldoc.Load(stream);
                            XmlNode node = xmldoc.SelectSingleNode("//sqlMaps");
                            if (node != null)
                            {
                                sb.Append(node.InnerXml);
                            }
                        }
                    }
                }
            }
            //else //没有配置文件
            //{
                //string[] sz = ModuleUtil.GetModules();
                //if (sz != null)
                //{
                //    foreach (string str in sz)
                //    {
                //        XmlDocument xmldoc = new XmlDocument();
                //        Stream stream = ResourceUtil.GetResourceStream(str, str + ".config.sqlmap.config");
                //        //加载所有数据配置文件
                //        if (stream != null)
                //        {
                //            xmldoc.Load(stream);
                //            XmlNode node = xmldoc.SelectSingleNode("//sqlMaps");
                //            if (node != null)
                //            {
                //                sb.Append(node.InnerXml);
                //            }
                //        }
                //    }
                //}

            //    string path = "config\\sqlmap.config";                
            //    //加载所有数据配置文件
            //    if (!string.IsNullOrEmpty(path))
            //    {
            //        XmlDocument xmldoc = new XmlDocument();
            //        xmldoc.Load(path);
            //        XmlNode node = xmldoc.SelectSingleNode("//sqlMaps");
            //        if (node != null)
            //        {
            //            sb.Append(node.InnerXml);
            //        }
            //    }
            //}
            sqlMaps = sb.ToString();

            sb = new StringBuilder();
            sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?><sqlMapConfig xmlns=\"http://ibatis.apache.org/dataMapper\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
            sb.AppendLine("<settings>");
            sb.AppendLine("<setting useStatementNamespaces=\"false\" />");
            sb.AppendLine("<setting cacheModelsEnabled=\"true\" />");
            sb.AppendLine("<setting validateSqlMap=\"true\" />");
            sb.AppendLine("</settings>");
            sb.AppendLine("<providers embedded=\"MyLib.providers.config,MyLib\" />");
            sb.AppendLine("<database>");
            sb.AppendLine("<provider name=\"${providerName}\"/>");
            sb.AppendLine("<dataSource name=\"db\" connectionString=\"${connectionString}\" />");
            sb.AppendLine("</database>");
            if (!string.IsNullOrEmpty(sqlMaps))
            {
                sb.AppendLine("<sqlMaps>" + sqlMaps + "</sqlMaps>");
            }
            sb.AppendLine("</sqlMapConfig>");

            return sb.ToString();

        }
    }
}
